//
//  BezziDifferentfoldLayout.swift
//  demo2-大小不一堆叠collectionView
//
//  Created by 刘文超 on 2020/5/30.
//  Copyright © 2020 刘文超. All rights reserved.
//

import UIKit

struct BezziDifferentFoldConstants {
    struct Cell {
        static let standCellHeight:CGFloat = 100
        static let expectCellHeight:CGFloat = 280
    }
}


class BezziDifferentfoldLayout: UICollectionViewLayout {
    
    let dragOffset:CGFloat = 180.0
    var caches:[UICollectionViewLayoutAttributes] = []
    
    //Q:有疑问不知道这两个为啥要这样计算，看不懂？
    //当前cell索引
    var featureItemIndex:Int {
        return max(0, Int(collectionView!.contentOffset.y / dragOffset))
    }
    var nextItemPercentageOffset:CGFloat {
        return (collectionView!.contentOffset.y / dragOffset) - CGFloat(featureItemIndex)
    }
    
    
    //
    var numbers:Int {
        return collectionView!.numberOfItems(inSection: 0)
    }
    
    var width:CGFloat{
        return collectionView!.frame.size.width
    }
    var height:CGFloat {
        return collectionView!.frame.size.height
    }
    
}

extension BezziDifferentfoldLayout {
    override var collectionViewContentSize: CGSize {
        //Q:这个是什么逻辑，没搞懂
        let contentSize = (CGFloat(numbers) * dragOffset) + (height - dragOffset)
        return CGSize(width: width, height: contentSize)
    }
    
    override func prepare() {
        caches.removeAll(keepingCapacity: false)
        
        let biaozhunHeight = BezziDifferentFoldConstants.Cell.standCellHeight
        let teshuHeight = BezziDifferentFoldConstants.Cell.expectCellHeight
        
        var fram = CGRect.zero
        var y:CGFloat = 0.0
        
        for item in 0..<numbers {
            let indexPath = IndexPath(item: item, section: 0)
            let attribute = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            
            attribute.zIndex = item
            var cellHeight = biaozhunHeight
            //Q:这里的计算逻辑也没有看懂，不知道什么意思？
            if indexPath.item == featureItemIndex {
                // The featured cell
                let yOffset = biaozhunHeight * nextItemPercentageOffset
                y = collectionView!.contentOffset.y - yOffset
                cellHeight = teshuHeight
            }else  if indexPath.item == (featureItemIndex + 1) && indexPath.item != numbers {
                // The cell directly below the featured cell, which grows as the user scrolls
                let maxY = y + biaozhunHeight
                cellHeight = biaozhunHeight + max((teshuHeight - biaozhunHeight) * nextItemPercentageOffset, 0)
                y = maxY - cellHeight
            }
            
            fram = CGRect(x: 0, y: y, width: width, height: cellHeight)
            attribute.frame = fram
            caches.append(attribute)
            y = fram.maxY
        }
        
     
    }
    

    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var attributs:[UICollectionViewLayoutAttributes] = []
        for item in caches {
            if item.frame.intersects(rect) {
                attributs.append(item)
            }
        }
        return attributs
        
    }
    
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        let itemIndex = round(proposedContentOffset.y / dragOffset)
        let yOffset = itemIndex * dragOffset
        return CGPoint(x: 0, y: yOffset)
    }
    
    //防止用户在滚动时连续无效
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
    
    
}
